<?php
include('pdf.php');
include('pdf_sent.php');

function create_report($ans){
    $pdf=new PDF('L','mm','A4');
    $pdf->AddFont('tahoma','','tahoma.php');
    $pdf->AddFont('tahoma','B','tahomabd.php');

	$count = 0;
	$start = 0;
	$stop = 0;
	$depart = $ans[0]['depart_id_recieve'];
	foreach($ans as $dat){
		if($dat['depart_id_recieve']==$depart){
			$stop = $count;
			$count = $count+1;
		}else{
			$dat2 = array_slice($ans,$start,$stop-$start+1);
			$start = $count;
			$stop = $count;
			$count = $count+1;
			$depart2 = $depart;
			$depart = $dat['depart_id_recieve'];
			createPDF($dat2,$pdf);
		}
			
	}
	$dat2 = array_slice($ans,$start,$stop-$start+1);
	createPDF($dat2,$pdf);
	$pdf->Output();
}

function create_report_nokasean($ans){
    $pdf=new PDF('L','mm','A4');
    $pdf->AddFont('tahoma','','tahoma.php');
    $pdf->AddFont('tahoma','B','tahomabd.php');

	$count = 0;
	$start = 0;
	$stop = 0;
	$depart = $ans[0]['depart_id_recieve'];
	foreach($ans as $dat){
		if($dat['depart_id_recieve']==$depart){
			$stop = $count;
			$count = $count+1;
		}else{
			$dat2 = array_slice($ans,$start,$stop-$start+1);
			$start = $count;
			$stop = $count;
			$count = $count+1;
			$depart2 = $depart;
			$depart = $dat['depart_id_recieve'];
			createPDF_nokasean($dat2,$pdf);
		}
			
	}
	$dat2 = array_slice($ans,$start,$stop-$start+1);
	createPDF_nokasean($dat2,$pdf);
	$pdf->Output();
}

function create_report_sent($ans){
    $pdf=new PDF_sent('L','mm','A4');
    $pdf->AddFont('tahoma','','tahoma.php');
    $pdf->AddFont('tahoma','B','tahomabd.php');

	$count = 0;
	$start = 0;
	$stop = 0;
	$depart = $ans[0]['depart_id_sent'];
	foreach($ans as $dat){
		if($dat['depart_id_sent']==$depart){
			$stop = $count;
			$count = $count+1;
		}else{
			$dat2 = array_slice($ans,$start,$stop-$start+1);
			$start = $count;
			$stop = $count;
			$count = $count+1;
			$depart2 = $depart;
			$depart = $dat['depart_id_sent'];
			create_report_sent_doctypesent($dat2,$pdf);
		}
			
	}
	$dat2 = array_slice($ans,$start,$stop-$start+1);
	create_report_sent_doctypesent($dat2,$pdf);
	$pdf->Output();
}

function create_report_sent_doctypesent($ans,$pdf){
	$count = 0;
	$start = 0;
	$stop = 0;
	$doctypesent = $ans[0]['edoc_typesent_id'];
	foreach($ans as $dat){
		if($dat['edoc_typesent_id']==$doctypesent){
			$stop = $count;
			$count = $count+1;
		}else{
			$dat2 = array_slice($ans,$start,$stop-$start+1);
			$start = $count;
			$stop = $count;
			$count = $count+1;
			createPDF_sent($dat2,$pdf,$doctypesent);
			$doctypesent = $dat['edoc_typesent_id'];
		}
			
	}
	$dat2 = array_slice($ans,$start,$stop-$start+1);
	createPDF_sent($dat2,$pdf,$doctypesent);
}

function create_report2($ans){
    $pdf=new PDF('L','mm','A4');
    $pdf->AddFont('tahoma','','tahoma.php');
    $pdf->AddFont('tahoma','B','tahomabd.php');

	$count = 0;
	$start = 0;
	$stop = 0;
	$depart = $ans[0]['depart_id_recieve'];
	foreach($ans as $dat){
		if($dat['depart_id_recieve']==$depart){
			$stop = $count;
			$count = $count+1;
		}else{
			$dat2 = array_slice($ans,$start,$stop-$start+1);
			$start = $count;
			$stop = $count;
			$count = $count+1;
			$depart2 = $depart;
			$depart = $dat['depart_id_recieve'];
			createPDF2($dat2,$pdf);
		}
			
	}
	$dat2 = array_slice($ans,$start,$stop-$start+1);
	createPDF2($dat2,$pdf);
	$pdf->Output();
}

function createPDF($dat,$pdf){
    $i=0;
    $recieve = array();
    $depart_r = iconv("UTF-8","ISO-8859-11",$dat[0]['depart_name_recieve']);
    foreach($dat as $row){
        $recieve[$i][0] = $row['recieve_no']."\n\n";
        $recieve[$i][1] = iconv("UTF-8","ISO-8859-11",$row['doc_no']."\n\n");
        $recieve[$i][2] = dateThDeco($row['doc_date']);
        $recieve[$i][3] = iconv("UTF-8","ISO-8859-11",xshDepartSendFast($row['depart_send_id'],$row['depart_name_send'],$row['depart_send_name_out'])."\n\n");
        $recieve[$i][4] = iconv("UTF-8","ISO-8859-11",$row['reciever']."\n\n");
        $recieve[$i][5] = iconv("UTF-8","ISO-8859-11",practise($row['edoc_id'],$row['depart_id_recieve'],$row['headline'])."\n");
        $recieve[$i][6] = iconv("UTF-8","ISO-8859-11",$row['comment']."\n\n");
        $recieve[$i][7] = dateThDeco($row['recieve_date']);
        $i=$i+1;
    }
    $pdf->addTable($recieve,$depart_r);
}

function createPDF_nokasean($dat,$pdf){
    $i=0;
    $recieve = array();
    $depart_r = iconv("UTF-8","ISO-8859-11",$dat[0]['depart_name_recieve']);
    foreach($dat as $row){
        $recieve[$i][0] = $row['recieve_no']."\n\n";
        $recieve[$i][1] = iconv("UTF-8","ISO-8859-11",$row['doc_no']."\n\n");
        $recieve[$i][2] = dateThDeco($row['doc_date']);
        $recieve[$i][3] = iconv("UTF-8","ISO-8859-11",xshDepartSendFast($row['depart_send_id'],$row['depart_name_send'],$row['depart_send_name_out'])."\n\n");
        $recieve[$i][4] = iconv("UTF-8","ISO-8859-11",$row['reciever']."\n\n");
        $recieve[$i][5] = iconv("UTF-8","ISO-8859-11",$row['headline']."\n");
        $recieve[$i][6] = iconv("UTF-8","ISO-8859-11",$row['comment']."\n\n");
        $recieve[$i][7] = dateThDeco($row['recieve_date']);
        $i=$i+1;
    }
    $pdf->addTable($recieve,$depart_r);
}

function createPDF_sent($dat,$pdf,$doctypesent){
	$doctypesentname = get_doctypesent_name($doctypesent);
    $i=0;
    $recieve = array();
    $depart_r = iconv("UTF-8","ISO-8859-11",$dat[0]['depart_name_sent']);
    foreach($dat as $row){
        $recieve[$i][0] = iconv("UTF-8","ISO-8859-11",$row['doc_no']."\n\n");
        $recieve[$i][1] = dateThDeco($row['doc_date']);
        $recieve[$i][2] = iconv("UTF-8","ISO-8859-11",xshDepartSendFast($row['depart_send_id'],$row['depart_name_send'],$row['depart_send_name_out'])."\n\n");
        $recieve[$i][3] = iconv("UTF-8","ISO-8859-11",$row['reciever']."\n\n");
        $recieve[$i][4] = iconv("UTF-8","ISO-8859-11",$row['headline']."\n");
        $recieve[$i][5] = iconv("UTF-8","ISO-8859-11",$row['comment']."\n\n");
        $recieve[$i][6] = dateThDeco($row['sent_date']);
        $i=$i+1;
    }
    $pdf->addTable($recieve,$depart_r,iconv("UTF-8","ISO-8859-11",$doctypesentname));
}

function createPDF2($dat,$pdf){
    $i=0;
    $recieve = array();
    $depart_r = iconv("UTF-8","ISO-8859-11",$dat[0]['depart_name_recieve']);
    foreach($dat as $row){
        $recieve[$i][0] = $row['recieve_no']."\n\n";
        $recieve[$i][1] = iconv("UTF-8","ISO-8859-11",$row['doc_no']."\n\n");
        $recieve[$i][2] = dateThDeco($row['doc_date']);
        $recieve[$i][3] = iconv("UTF-8","ISO-8859-11",xshDepartSendFast($row['depart_send_id'],$row['depart_name_send'],$row['depart_send_name_out'])."\n\n");
        $recieve[$i][4] = iconv("UTF-8","ISO-8859-11",$row['reciever']."\n\n");
        $recieve[$i][5] = iconv("UTF-8","ISO-8859-11",practise2($row['edoc_id'],$row['depart_id_recieve'],$row['headline'])."\n");
        $recieve[$i][6] = iconv("UTF-8","ISO-8859-11",$row['comment']."\n\n");
        $recieve[$i][7] = dateThDeco($row['recieve_date']);
        $i=$i+1;
    }
    $pdf->addTable($recieve,$depart_r);
}

function no_pdf(){
    $pdf=new PDF('L','mm','A4');
    $pdf->AddFont('tahoma','','tahoma.php');
    $pdf->AddFont('tahoma','B','tahomabd.php');
    $pdf->AddPage();
    $pdf->Output();
}

function wcut($str,$co){
    $r = "";
    $i = 0;

    for($c=0;$c<iconv_strlen($str);$c++){
        $txt = iconv_substr($str,$c,1);
        $r = $r.$txt;
        if($txt=="\n"){
            $i = 0;
        }
        if(nocountwcut($txt)) continue;
        $i=$i+1;
        if($i>=$co){
            $i = 0;
            $r = $r."\n";
        }
    }
    return $r;
}

function nocountwcut($txt){
    return false;
}
?>
